<?php

namespace app\admin\controller;

use app\admin\service\TiktokService;
use think\facade\Request;
use app\common\Common;

// 抖音类
class Tiktok extends Base
{
    private $service;
    public function __construct(TiktokService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    /**
     * 退款回调。
     */
    public function refundCallback()
    {
        $params = Request::param();
        $log = fopen('./tiktok_refund_callback.txt', 'a+');
        fwrite($log, json_encode($params) . PHP_EOL);
        fclose($log);
    }

    public function newRefundCallback()
    {
        $params = Request::param();
        flog([
            'params' => $params,
        ],'dy_new_refund_back');
        $result = $this->service->newRefundCallback($params);
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit();
    }
    /**
    * 查询任务台任务投稿视频数据（明细）
    */
    public function getTaskVideo()
    {
        $params = Request::param();
        $result = $this->service->getTaskVideoDaily($params);

        if ($result['err'] == 1) {
            return Common::fail($result['msg']);
        }

        return Common::success($result['data']);
    }
    /**
    * 查询任务台任务投稿视频数据按月统计总数
    */
    public function getTaskVideoMonth()
    {
        $params = Request::param();
        $result = $this->service->getTaskVideoDailyMonth($params);

        if ($result['err'] == 1) {
            return Common::fail($result['msg']);
        }

        return Common::success($result['data']);
    }
      // 任务台数据导出
      public function monthLeadingOut()
      {
          $params = Request::param();
          $message = $this->service->monthLeadingOut($params);
          return Common::success();
      }
    /**
     *查询任务台单个任务投稿视频数据
     */
    public function getTaskVideoData()
    {
        $params = Request::param();
        $result = $this->service->getTaskVideoData($params);

        if ($result['err'] == 1) {
            return Common::fail($result['msg']);
        }

        return Common::success($result['data']);
    }
    /**
     * 抖音撮合合作商和达人列表页面
     */
    public function getClientList()
    {
        $params = Request::param();

        $data = $this->service->getClientList($params);

        return Common::success($data);
    }

    // 添加抖音撮合合作商和达人
    public function addClient()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Client::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->addClient($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    // 修改抖音撮合合作商和达人
    public function editClient()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Client::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->addClient($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    // 删除抖音撮合合作商和达人
    public function delClient()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Client::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->addClient($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    // 抖音撮合合作商和达人下拉列表
    public function selectClientList()
    {
        $params = Request::param();
        $data = $this->service->getClientSelectList($params);

        return Common::success($data);
    }
    // 抖音撮合合作商和达人详情
    public function detailClientList()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\Client::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $data = $this->service->getClientDetail($params['id']);

        return Common::success($data);
    }
}
